const { resolve } = require('./myPromise');
const MyPromise = require('./myPromise');
//Promise就是一个类，执行类时，需要传递执行器进去，且执行器会立即执行
//三种状态，pending,fulfilled,rejected
//resolve和reject是用来更改状态的

let promise1 = new MyPromise((resolve, reject) => {
    resolve('promise1成功')
    reject('promise1失败')

})
//测试then方法中成功和失败回调
//then成功和失败都有个参数
promise1.then(value => {
    console.log(value)
}, reason => {
    console.log(reason)
})

//异步代码测试
//异步中resolve/reject都是异步执行的话，而then立即执行的话状态是pedding
let promise2 = new MyPromise((resolve, reject) => {
    setTimeout(()=>{
        resolve('promise2成功')
    },2000)


    // setTimeout(() => {
    //     reject('promise2失败')
    // }, 2000)

})
promise2.then(value => {
    console.log(value)
}, reason => {
    console.log(reason)
})



//测试then方法链式调用
//考虑异步情况
//将上一个方法的回调值传给下一个方法
//then可以返回promise对象，但不能返回当前promise的then方法返回的promise对象
let promise3 = new MyPromise((resolve, reject) => {
    // setTimeout(()=>{
    //     resolve('promise3成功')
    // },2000)
    resolve('promise3成功')

    // setTimeout(() => {
    //     reject('promise3失败')
    // }, 2000)

})
function other(){
    return new MyPromise ((resolve,reject)=>{
        resolve('other')
    })
}
let p3=promise3.then(value => {
    console.log(value)
    return p3
});
p3.then(value => {
    
    console.log(value)

}, reason => {
    console.log(reason)
})

//测试then方法参数是否可选
let promise4 = new MyPromise((resolve, reject) => {

    //resolve('promise4成功')
    reject('promise4失败')



})
promise4.then()
.then()
.then()
.then(value=>console.log(value),reason=>console.log(reason))

//测试promise.all
let p5 = new MyPromise((resolve, reject) => {

    resolve('promise5成功')
    //reject('promise5失败')



})
let p6 = new MyPromise((resolve, reject) => {

    resolve('promise6成功')
    //reject('promise6失败')



})
Promise.all(['A',p5,p6,'B']).then(value=>console.log(value))
//
Promise.resolve(1000).then(value=>console.log(value))
Promise.resolve(promise2).then(value=>console.log(value))





